import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Time:21:46
 */
public class demo6 {
    public List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> ret=new ArrayList<>();
        char[] s=ss.toCharArray();
        char[] p=pp.toCharArray();
        int[] hash1 = new int[26];
        for(char ch:p){
            hash1[ch-'a']++;
        }
        int[] hash2=new int[26];
        int n=s.length;
        int m=p.length;
        for(int left=0,right=0,count=0;right<n;right++){
            int in=s[right];
            if(++hash2[in-'a']<=hash1[in-'a']){
                count++;
            }
            if(right-left+1>m){
                int out=s[left++];
                if(hash2[out-'a']--<=hash1[out-'a']){
                    count--;
                }
            }
            if(count==m){//更新结果。
                ret.add(left);
            }
        }
        return ret;
    }
}
